package com.affablebeans.model.bll;

import java.sql.SQLException;
import java.util.List;

import com.affablebeans.model.dal.AbstractDAL;
import com.affablebeans.model.entity.GenericEntity;

/**
 * 
 * @author Bora SAYINER
 * @since Jan 26, 2015 11:16:46 PM
 * @version 1.0.0
 */
public class GenericEntityBLL<E extends GenericEntity> {

	private AbstractDAL<E> abstractDAL;
	private final EntityValidator<E> entityValidator;

	public GenericEntityBLL(AbstractDAL<E> abstractDAL, EntityValidator<E> entityValidator) {
		this.abstractDAL = abstractDAL;
		this.entityValidator = entityValidator;
	}

	public boolean insertEntity(E entity) throws SQLException {
		boolean result = false;
		if (entityValidator.validate(entity)) result = abstractDAL.insert(entity);
		return result;
	}

	public boolean updateEntity(E entity) throws SQLException {
		boolean result = false;
		if (entityValidator.validate(entity)) result = abstractDAL.update(entity);
		return result;
	}

	public boolean deleteEntity(E entity) throws SQLException {
		return abstractDAL.delete(entity);
	}

	public E findEntityById(int... id) throws SQLException {
		return abstractDAL.findById(id);
	}

	public List<E> getAllEntities() throws SQLException {
		return abstractDAL.getAll();
	}

	public int getEntitiesCount() throws SQLException {
		return abstractDAL.getCount();
	}

}
